package com.bupt.liutong.admin.shop.bo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.shop.dao.AttrDao;
import com.bupt.liutong.admin.shop.dto.Attr;
import com.bupt.liutong.admin.shop.form.AttrForm;
import com.bupt.liutong.util.BeanUtils;

public class AttrBo extends BaseBo {

	private AttrDao attrDao;

	public List<AttrForm> getAttrsByPage(AttrForm attrForm,
			HttpServletRequest request) {
		Attr attr = new Attr();
		sm.pagingWithNoCriteria(attrForm, attr, "shop_attr.getAttrsCount");
		List<Attr> attrs = attrDao.getAttrsByPage(attr);
		return BeanUtils.dtoToForm(attrs, AttrForm.class);
	}

	public AttrForm getAttrById(AttrForm attrForm) {
		AttrForm resultForm = new AttrForm();
		Attr attr = attrDao.getAttrById(attrForm.getId());
		BeanUtils.copyProperties(resultForm, attr);
		return resultForm;
	}

	public List<AttrForm> getAttrsByTypeId(AttrForm attrForm) {
		List<Attr> attrs = attrDao.getAttrsByTypeId(attrForm.getTypeId());
		return BeanUtils.dtoToForm(attrs, AttrForm.class);
	}

	@Transactional
	public void insertAttr(AttrForm attrForm) {
		Attr attr = new Attr();
		BeanUtils.copyProperties(attr, attrForm);
		attrDao.insertAttr(attr);
	}

	@Transactional
	public void updateAttr(AttrForm attrForm) {
		Attr attr = new Attr();
		BeanUtils.copyProperties(attr, attrForm);
		attrDao.updateAttr(attr);
	}

	@Transactional
	public void deleteAttrs(String[] ids) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				attrDao.deleteAttr(Integer.parseInt(ids[i]));
			}
		}
	}

	public void setAttrDao(AttrDao attrDao) {
		this.attrDao = attrDao;
	}
}
